home *** CD-ROM | disk | FTP | other *** search
- /*
- File: TDrawContext.h
-
- Contains: Interface file for a QuickDraw layer class
-
- Written by: Arno Gourdol
-
- Copyright: © 1994-1995 by Apple Computer, Inc., all rights reserved.
-
- */
-
- #pragma once
-
- #ifndef __TDRAWCONTEXT__
- #define __TDRAWCONTEXT__
-
- #include "assert.h"
-
- #include <Quickdraw.h>
- #include "CRect.h"
- #include "CColor.h"
-
-
-
- //
- // Class TDrawContext
- //
- // Contains related graphical information:
- // - GrafPort
- // - Background and Foreground colors
- // - Pen size, mode, position, pattern
-
- class CTempRgn;
-
- class TBitmap;
-
- class TDrawContext
- {
- friend class TDrawContextIterator;
- public:
- // constructors
- inline TDrawContext(void);
- inline TDrawContext(GrafPtr grafPtr, UInt16 depth = 1, Boolean inColor = true);
-
- // destructor
- inline ~TDrawContext(void);
-
- // accessor
- inline CRect Bounds(void) const;
- inline Boolean IsColor(void) const;
- inline UInt16 GetDepth(void) const;
-
- // setters
- inline void SetPenSize(GraphicalUnit size) const;
- void SetHighColor(CColor color);
-
- inline void ConvertToScreen(CPoint& point) const;
- inline void ConvertFromScreen(CPoint& point) const;
- inline void ConvertToScreen(CRect& rect) const;
- inline void ConvertFromScreen(CRect& rect) const;
-
- // Pen location
- void MovePenTo(CPoint pt);
- void MovePenTo(GraphicalUnit x, GraphicalUnit y);
- void MovePenBy(GraphicalUnit dx, GraphicalUnit dy);
- CPoint PenLocation(void) const;
-
- // Drawing lines
- void StrokeLine(CPoint toPt);
- void StrokeLine(CPoint pt0, CPoint pt1);
-
- // Drawing Rectangles
- void StrokeRect(const CRect& rect);
- void FillRect(const CRect& rect);
- void InvertRect(const CRect& rect);
-
- // Drawing Regions
- void StrokeRegion(const RgnHandle rgn);
- void FillRegion(const RgnHandle rgn);
-
- // Drawing bitmaps
- void DrawBitmap(const TBitmap* bitmap) const;
- void DrawBitmap(const TBitmap* bitmap, CPoint where) const;
- void DrawBitmap(const TBitmap* bitmap, CRect dstRect) const;
- void DrawBitmap(const TBitmap* bitmap, CRect dstRect, RgnHandle mask) const;
- void DrawBitmap(const TBitmap* bitmap, CRect srcRect, CRect dstRect) const;
-
- // Drawing focus rings
- void StrokeFocusRing(const CRect& rect);
- void StrokeFocusRing(const RgnHandle rgn);
-
- //
- virtual Boolean Lock(void);
- virtual void Unlock(void);
-
- protected:
- inline Boolean IsColorPort(void) const;
-
- GrafPtr fSavePort; // GrafPort previously set
- PenState fSavePenState; // Pen size, mode, pattern...
- RGBColor fSaveForeColor;
- RGBColor fSaveBackColor;
-
- GrafPtr fPort;
- UInt16 fDepth;
- Boolean fIsColor;
-
- SInt16 fLockCount;
- };
-
-
- // Input Iterator
- class TDrawContextIterator
- {
- public:
- inline TDrawContextIterator(void);
- inline TDrawContextIterator(const CRect& area);
- inline TDrawContextIterator(GrafPtr port, const CRect& area);
- inline ~TDrawContextIterator();
-
- inline TDrawContextIterator& operator ++();
- inline TDrawContext& operator *(void);
- inline Boolean operator ==(const TDrawContextIterator& operand) const;
- inline Boolean operator !=(const TDrawContextIterator& operand) const;
- inline TDrawContextIterator& end(void) const;
- private:
- GDHandle fCurrentDevice;
- CRect fArea;
- TDrawContext fDrawContext;
- };
-
-
- inline TDrawContextIterator& TDrawContextIterator::end(void) const
- {
- TDrawContextIterator nullIterator;
- return nullIterator;
- }
-
-
- inline Boolean TDrawContextIterator::operator ==(const TDrawContextIterator& operand) const
- {
- return (fCurrentDevice == operand.fCurrentDevice);
- }
-
- inline Boolean TDrawContextIterator::operator !=(const TDrawContextIterator& operand) const
- {
- return (fCurrentDevice != operand.fCurrentDevice);
- }
-
-
- inline TDrawContextIterator::TDrawContextIterator(void)
- {
- fCurrentDevice = NULL;
- }
-
-
- inline TDrawContextIterator::TDrawContextIterator(const CRect& area)
- {
- fCurrentDevice = GetDeviceList();
- fArea = area;
- if (fDrawContext.Lock())
- {
- fDrawContext.ConvertToScreen(fArea);
- fDrawContext.Unlock();
- }
- while (fCurrentDevice != NULL && !fArea.Intersects(CRect((**fCurrentDevice).gdRect)))
- {
- fCurrentDevice = GetNextDevice(fCurrentDevice);
- }
-
- if (fCurrentDevice != NULL)
- {
- fDrawContext.fDepth = (**(**fCurrentDevice).gdPMap).pixelSize;
- fDrawContext.fIsColor = ((**fCurrentDevice).gdFlags & 1) != 0;
- }
- }
-
- inline TDrawContextIterator::TDrawContextIterator(GrafPtr port, const CRect& area) :
- fDrawContext(port)
- {
- fCurrentDevice = GetDeviceList();
- fArea = area;
- if (fDrawContext.Lock())
- {
- fDrawContext.ConvertToScreen(fArea);
- fDrawContext.Unlock();
- }
-
- while (fCurrentDevice != NULL && !fArea.Intersects(CRect((**fCurrentDevice).gdRect)))
- {
- fCurrentDevice = GetNextDevice(fCurrentDevice);
- }
-
- if (fCurrentDevice != NULL)
- {
- fDrawContext.fDepth = (**(**fCurrentDevice).gdPMap).pixelSize;
- fDrawContext.fIsColor = ((**fCurrentDevice).gdFlags & 1) != 0;
- }
- }
-
-
-
- inline TDrawContextIterator::~TDrawContextIterator()
- {
- }
-
-
- inline TDrawContextIterator& TDrawContextIterator::operator ++()
- {
-
- // Look at the next device
- do
- {
- fCurrentDevice = GetNextDevice(fCurrentDevice);
- } while (fCurrentDevice != NULL && !fArea.Intersects(CRect((**fCurrentDevice).gdRect)));
-
- if (fCurrentDevice != NULL)
- {
- fDrawContext.fDepth = (**(**fCurrentDevice).gdPMap).pixelSize;
- fDrawContext.fIsColor = ((**fCurrentDevice).gdFlags & 1) != 0;
- }
- return *this;
- }
-
-
- inline TDrawContext& TDrawContextIterator::operator *(void)
- {
- return fDrawContext;
- }
-
-
-
- //
- // Inlines for TDrawContext
- //
-
- inline TDrawContext::TDrawContext(void) :
- fSavePort(NULL),
- fLockCount(0)
- {
- GetPort(&fPort);
- fDepth = 1;
- fIsColor = false;
- }
-
- inline TDrawContext::TDrawContext(GrafPtr grafPtr, UInt16 depth, Boolean inColor) :
- fSavePort(NULL),
- fLockCount(0)
- {
- fPort = grafPtr;
- fDepth = depth;
- fIsColor = inColor;
- }
-
- inline TDrawContext::~TDrawContext()
- {
- assert(fLockCount == 0);
- }
-
- inline CRect TDrawContext::Bounds(void) const
- {
- CRect result(fPort->portRect);
- ConvertToScreen(result);
- return result;
- }
-
- inline Boolean TDrawContext::IsColor(void) const
- {
- return fIsColor;
- }
-
- inline Boolean TDrawContext::IsColorPort(void) const
- {
- return (fPort->portBits.rowBytes & 0xC000) == 0xC000;
- }
-
- inline UInt16 TDrawContext::GetDepth(void) const
- {
- return fDepth;
- }
-
- inline void TDrawContext::SetPenSize(GraphicalUnit size) const
- {
- assert(fLockCount > 0);
-
- ::PenSize(size, size);
- }
-
- inline void TDrawContext::ConvertToScreen(CPoint& point) const
- {
- assert(fLockCount > 0);
-
- ::LocalToGlobal(point);
- }
-
- inline void TDrawContext::ConvertFromScreen(CPoint& point) const
- {
- assert(fLockCount > 0);
-
- ::GlobalToLocal(point);
- }
-
- inline void TDrawContext::ConvertToScreen(CRect& rect) const
- {
- assert(fLockCount > 0);
-
- CPoint temp = rect.LeftTop();
- ::LocalToGlobal(temp);
- rect.SetLeftTop(temp);
- temp = rect.RightBottom();
- ::LocalToGlobal(temp);
- rect.SetRightBottom(temp);
- }
-
-
- inline void TDrawContext::ConvertFromScreen(CRect& rect) const
- {
- assert(fLockCount > 0);
-
- // ??? Doesn't work LefTop() returns a cons See ConverToScreen
- ::GlobalToLocal(rect.LeftTop());
- ::GlobalToLocal(rect.RightBottom());
- }
-
-
- //
- // CClip class
- //
- // Class to save, restore and change the clipping region
-
- class CClip
- {
- public:
- inline CClip();
- inline ~CClip();
- inline void Save();
- inline void Restore();
- inline void Set(RgnHandle clipRgn);
- inline void Set(const Rect& clipRect);
- inline void Set(const CRect& clipRect);
-
- private:
- RgnHandle fClipRgn;
- #ifndef NDEBUG
- short fSaveCount;
- #endif
-
- };
-
- //
- // CTempRgn class
- //
- // Class managing a temporary region. Useful to pass
- // rects to routines require regions.
- // Example:
- // CTempRgn aRgn(r);
- // SetClip(aRgn);
-
- class CTempRgn
- {
- public:
- // constructors
- inline CTempRgn();
- inline CTempRgn(const Rect& area);
- inline CTempRgn(const CTempRgn& rgn); // copy constructor
- inline CTempRgn(const RgnHandle rgn);
-
- // destructor
- inline ~CTempRgn(void);
-
- // operators
- inline operator RgnHandle(void);
-
- // modifiers
- inline void Set(const CRect& bounds);
- inline void LocalToGlobal(void);
- inline void GlobalToLocal(void);
-
- // selectors
- inline CRect Bounds(void) const;
-
- private:
- // member objects
- RgnHandle fTempRgn;
- };
-
-
- //
- // Inlines for CClip
- //
-
- inline CClip::CClip()
- {
- fClipRgn = NULL;
- #ifndef NDEBUG
- fSaveCount = 0;
- #endif
-
- }
-
-
- inline CClip::~CClip()
- {
- if (fClipRgn != NULL)
- DisposeRgn(fClipRgn);
- #ifndef NDEBUG
- assert(fSaveCount == 0);
- #endif
-
- }
-
-
- inline void CClip::Set(RgnHandle clipRgn)
- {
- SetClip(clipRgn);
- }
-
-
-
- inline void CClip::Set(const Rect& clipRect)
- {
- ClipRect(&clipRect);
- }
-
-
- inline void CClip::Set(const CRect& clipRect)
- {
- ClipRect(clipRect);
- }
-
-
-
- inline void CClip::Save()
- {
- if (fClipRgn == NULL)
- fClipRgn = NewRgn();
- GetClip(fClipRgn);
- #ifndef NDEBUG
- fSaveCount++;
- #endif
-
- }
-
-
- inline void CClip::Restore()
- {
- SetClip(fClipRgn);
- #ifndef NDEBUG
- fSaveCount--;
- #endif
-
- }
-
-
- //
- // Inlines for CTempRgn
- //
-
-
- inline CTempRgn::operator RgnHandle(void)
- {
- return fTempRgn;
- }
-
- inline CTempRgn::CTempRgn(const Rect& area)
- {
- fTempRgn = NewRgn();
- RectRgn(fTempRgn, &area);
- }
-
- inline CTempRgn::CTempRgn(const CTempRgn& rgn)
- {
- fTempRgn = NewRgn();
- CopyRgn((CTempRgn&)rgn, fTempRgn); // (-) const
- }
-
- inline CTempRgn::CTempRgn(const RgnHandle rgn)
- {
- fTempRgn = NewRgn();
- CopyRgn((RgnHandle)rgn, fTempRgn);// (-)
- }
-
- inline CTempRgn::CTempRgn()
- {
- fTempRgn = NewRgn();
- }
-
-
- inline CTempRgn::~CTempRgn(void)
- {
- assert(fTempRgn != NULL);
- DisposeRgn(fTempRgn);
- }
-
- inline void CTempRgn::Set(const CRect& bounds)
- {
- RectRgn(fTempRgn, bounds);
- }
-
- inline void CTempRgn::LocalToGlobal(void)
- {
- ::LocalToGlobal((Point *) &((*fTempRgn)->rgnBBox.top));
- ::LocalToGlobal((Point *) &((*fTempRgn)->rgnBBox.bottom));
- }
-
-
- inline void CTempRgn::GlobalToLocal(void)
- {
- ::GlobalToLocal((Point *) &((*fTempRgn)->rgnBBox.top));
- ::GlobalToLocal((Point *) &((*fTempRgn)->rgnBBox.bottom));
- }
-
- inline CRect CTempRgn::Bounds(void) const
- {
- return (*fTempRgn)->rgnBBox;
- }
-
-
- #endif
-
-